﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actya.Web.Infrastructure.ElFinderConnector.Utils;

namespace Actya.Web.Infrastructure.ElFinderConnector
{
    public class Cdc
    {
        public string name { get; set; }

        public string hash { get; set; }

        public string url { get; set; }

        public string tmb { get; set; }

        public string date { get; set; }

        public string mime { get; set; }

        public long size { get; set; }

        public bool read { get; set; }

        public bool write { get; set; }

        public bool rm { get; set; }

        public Cdc(System.IO.DirectoryInfo dir)
        {
            this.date = dir.LastWriteTime.ToShortDateString();
            this.name = dir.Name;
            var rights = dir.GetRights();
            this.read = rights.canRead();
            this.write = this.rm = rights.canWrite();
            this.mime = "directory";
            this.size = this.read ? dir.Size() : 0;
            this.hash = dir.FullName.Hash();
            this.url = Configuration.Configuration.RootUrl + dir.PathFromRoot().Replace(@"\", "/");
            this.rm = rights.canDelete() && rights.canWrite();
        }

        public Cdc(System.IO.FileInfo file)
        {
        	var extensionWithoutDot = file.Extension.Length > 1 ? file.Extension.Substring(1) : file.Extension;
            this.date = file.LastWriteTime.ToShortDateString();
            this.name = file.Name;
			this.mime = Configuration.Configuration.AllowedMimeTypes.Where(mt => mt.Value.Contains(extensionWithoutDot.ToLower())).Select(mt => mt.Key).FirstOrDefault() ?? "application/unknown";                        
            var rights = file.GetRights();
            this.read = rights.canRead();
            this.write = this.rm = rights.canWrite();
            this.size = this.read ? file.Length : 0;
            this.hash = file.FullName.Hash();
            this.url = Configuration.Configuration.RootUrl + file.PathFromRoot().Replace(@"\", "/");
            if (file.IsImage())
            {
                this.tmb = file.ThumbUrl();
            }
        }
    }
}
